package com.atguigu.tingshu.account.service.impl;

import com.atguigu.tingshu.account.mapper.RechargeInfoMapper;
import com.atguigu.tingshu.account.service.RechargeInfoService;
import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.account.RechargeInfo;
import com.atguigu.tingshu.vo.account.RechargeInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.redisson.api.RBlockingDeque;
import org.redisson.api.RDelayedQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class RechargeInfoServiceImpl extends ServiceImpl<RechargeInfoMapper, RechargeInfo> implements RechargeInfoService {

	@Autowired
	private RechargeInfoMapper rechargeInfoMapper;

	@Override
	public String submitRecharge(RechargeInfoVo rechargeInfoVo) {

		// 获取当前登录的用户的id
		Long userId = AuthContextHolder.getUserId();

		// 生成一个uuid作为订单编号
		String orderNo = UUID.randomUUID().toString().replace("-", "");

		// 保存充值订单的数据
		RechargeInfo rechargeInfo = new RechargeInfo() ;
		rechargeInfo.setUserId(userId);
		rechargeInfo.setOrderNo(orderNo);
		rechargeInfo.setRechargeStatus(SystemConstant.ORDER_STATUS_UNPAID);
		rechargeInfo.setRechargeAmount(rechargeInfoVo.getAmount());
		rechargeInfo.setPayWay(rechargeInfoVo.getPayWay());
		rechargeInfoMapper.insert(rechargeInfo) ;

		// 发布延迟任务...实现订单超时未支付自动取消订单的功能


		return orderNo;
	}


	@Override
	public RechargeInfo getByOrderNo(String orderNo) {
		LambdaQueryWrapper<RechargeInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>() ;
		lambdaQueryWrapper.eq(RechargeInfo::getOrderNo , orderNo) ;
		RechargeInfo rechargeInfo = rechargeInfoMapper.selectOne(lambdaQueryWrapper);
		return rechargeInfo;
	}
}
